/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.flowable.test.spring.boot;

import static org.assertj.core.api.Assertions.assertThat;

import org.flowable.engine.ManagementService;
import org.flowable.engine.impl.cmd.AbstractCustomSqlExecution;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import flowable.Application;
import flowable.mappers.CustomMybatisMapper;

/**
 * @author Filip Hrisafov
 */
@SpringBootTest(classes = Application.class)
public class CustomMyBatisMappersApplicationTest {

    @Autowired
    private ManagementService managementService;

    @Test
    public void executeCustomMybatisMapperQuery() {
        String processDefinitionId = managementService.executeCustomSql(new AbstractCustomSqlExecution<CustomMybatisMapper, String>(CustomMybatisMapper.class) {
            @Override
            public String execute(CustomMybatisMapper customMybatisMapper) {
                return customMybatisMapper.loadProcessDefinitionIdByKey("waiter");
            }
        });

        assertThat(processDefinitionId)
            .as("Process definition id")
            .isNotNull();
    }

    @Test
    public void executeCustomMybatisXmlQuery() {
        String processDefinitionDeploymentId = managementService.executeCommand(commandContext ->
            (String) CommandContextUtil.getDbSqlSession(commandContext)
                .selectOne("selectProcessDefinitionDeploymentIdByKey", "waiter"));

        assertThat(processDefinitionDeploymentId)
            .as("Process deployment Id")
            .isNotNull();
    }
}
